import uuid
from eval_lib.common.exceptions import BadRequestException
from eval_lib.model.base import BaseStruct
from eval_lib.model.const import CASE_PARAMS_STATUS_CREATE, CASE_PARAMS_STATUS_PAUSE, CASE_PARAMS_STATUS_CANCEL, CASE_PARAMS_STATUS_RESUME, CASE_PARAMS_STATUS_FROCE_END
from config import conf

RUNNER_IMAGE_TAG_DEFAULT_LATEST = "stable"
AGENT_TYPE_DEFAULT_DEEPFLOWCE = "deepflowce"


class AutoTestCreate(BaseStruct):

    KEYS = [
        "uuid",
        "case_name",
        "process_num",
        "runner_image_tag",
        "agent_type",
        "agent_config",
        "case_variables",
        "user",
        "cron_id",
    ]

    def init(self, **kwargs):
        super().init(**kwargs)
        self.uuid = str(uuid.uuid4())
        self.runner_image_tag = RUNNER_IMAGE_TAG_DEFAULT_LATEST if not self.runner_image_tag else self.runner_image_tag
        self.agent_type = AGENT_TYPE_DEFAULT_DEEPFLOWCE if not self.agent_type else self.agent_type

    def is_valid(self):
        # TODO
        if not (self.uuid):
            raise BadRequestException("bad request")
        if self.agent_type not in conf.agent_tools:
            raise BadRequestException(
                f"bad request agent_type {self.agent_type}")


class AutoTestUpdate(BaseStruct):

    KEYS = ["uuids", "status"]

    def is_valid(self):
        # TODO
        if not self.uuids:
            raise BadRequestException("bad request no uuids")
        if self.status is not None:
            if self.status not in [
                    CASE_PARAMS_STATUS_CREATE,
                    CASE_PARAMS_STATUS_PAUSE,
                    CASE_PARAMS_STATUS_CANCEL,
                    CASE_PARAMS_STATUS_RESUME,
                    CASE_PARAMS_STATUS_FROCE_END,
            ]:
                raise BadRequestException(f"bad request status {self.status}")


class AutoTestDelete(BaseStruct):

    KEYS = ["uuids"]

    def is_valid(self):
        # TODO
        if not self.uuids:
            raise BadRequestException("bad request")


class AutoTestFilter(BaseStruct):

    KEYS = ["uuid", "uuids", "status", "page_size", "page_index"]

    def init(self, **kwargs):
        super().init(**kwargs)
        self.page_size = int(self.page_size) if self.page_size else None
        self.page_index = int(self.page_index) if self.page_index else None
